﻿#region Namespaces

using System.Collections.Generic;
using System.Linq;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Vibstudio.X4NET.Collections;
using Vibstudio.X4NET.Test.Util;
using Vibstudio.X4NET.Xml.Navigation;
using Vibstudio.X4NET.Xml.XPath;

#endregion

namespace Vibstudio.X4NET.Test
{
    [TestClass]
    public class XmlNavigationTests
    {
        [TestMethod]
        public void GetValue_NEW()
        {
            const string FILE_NAME = "SM2_0303_2015015.xml";
            const string XPATH = "/Prestazione/IdentificativiRichiesta[1]/cod_prat_distr[1]";
            const string EXPECTED_VALUE = "cod_prat_distr0";

            string uri = IOHelper.ComposeSampleFilePath(FILE_NAME).Path;

            string actual = XNavigator.Parse(uri).GetValue(XPATH);

            Assert.AreEqual(EXPECTED_VALUE, actual);
        }

        [TestMethod]
        public void GetKeyValuePairs_OLD1()
        {
            const string FILE_NAME = "SM2_0303_2015015.xml";

            string uri = IOHelper.ComposeSampleFilePath(FILE_NAME).Path;

            XPathList expected = new XPathList(new[]
                                               {
                                                   "/Prestazione/@cod_servizio",
                                                   "/Prestazione/@cod_flusso",
                                                   "/Prestazione/IdentificativiRichiesta[1]/piva_utente[1]",
                                                   "/Prestazione/IdentificativiRichiesta[1]/piva_distr[1]",
                                                   "/Prestazione/IdentificativiRichiesta[1]/cod_prat_utente[1]",
                                                   "/Prestazione/IdentificativiRichiesta[1]/cod_prat_distr[1]",
                                                   "/Prestazione/DatiTecnici[1]/cod_pdr[1]",
                                                   "/Prestazione/data_ese[1]"
                                               });

            XPathList actual = new XmlNavigator().GetKeyValuePairs(uri).XPaths;

            Assert.IsTrue(expected.IsMatch(actual));
        }

        [TestMethod]
        public void GetKeyValuePairs_NEW1()
        {
            const string FILE_NAME = "SM2_0303_2015015.xml";

            string uri = IOHelper.ComposeSampleFilePath(FILE_NAME).Path;

            XPathList expected = new XPathList(new[]
                                               {
                                                   "/Prestazione/@cod_servizio",
                                                   "/Prestazione/@cod_flusso",
                                                   "/Prestazione/IdentificativiRichiesta[1]/piva_utente[1]",
                                                   "/Prestazione/IdentificativiRichiesta[1]/piva_distr[1]",
                                                   "/Prestazione/IdentificativiRichiesta[1]/cod_prat_utente[1]",
                                                   "/Prestazione/IdentificativiRichiesta[1]/cod_prat_distr[1]",
                                                   "/Prestazione/DatiTecnici[1]/cod_pdr[1]",
                                                   "/Prestazione/data_ese[1]"
                                               });

            XPathList actual = XNavigator.Parse(uri).XPaths;

            Assert.IsTrue(expected.IsMatch(actual));
        }

        [TestMethod]
        public void GetKeyValuePairs_NEW2()
        {
            const string FILE_NAME = "SM2_0303_2015015_Plus.xml";

            string uri = IOHelper.ComposeSampleFilePath(FILE_NAME).Path;

            XPathList expectedXPaths = new XPathList(new[]
                                                     {
                                                         "/Prestazione/@cod_servizio",
                                                         "/Prestazione/@cod_flusso",
                                                         "/Prestazione/IdentificativiRichiesta[1]/piva_utente[1]",
                                                         "/Prestazione/IdentificativiRichiesta[1]/piva_distr[1]",
                                                         "/Prestazione/IdentificativiRichiesta[1]/cod_prat_utente[1]",
                                                         "/Prestazione/IdentificativiRichiesta[1]/cod_prat_distr[1]",
                                                         "/Prestazione/DatiTecnici[1]/cod_pdr[1]",
                                                         "/Prestazione/data_ese[1]"
                                                     });

            List<string> expectedValues = new XPathList(new[]
                                                        {
                                                            "SM2",
                                                            "0303",
                                                            "00000000000",
                                                            "cod_prat_utente",
                                                            "cod_prat_distr0",
                                                            "00000000000000",
                                                            "01/01/1900"
                                                        });

            XmlKeyValuePairCollection actual = XNavigator.Parse(uri, false, true);

            XPathList actualXPaths = actual.XPaths;
            Assert.IsTrue(expectedXPaths.IsMatch(actualXPaths));

            List<string> actualValues = new XPathList(actual.Select(valuePair => valuePair.Value));
            Assert.IsTrue(expectedValues.CompareList(actualValues));
        }

        [TestMethod]
        public void GetKeyValuePairs_NEW3()
        {
            const string FILE_NAME = "SM2_0303_2015015_Plus.xml";
            const string XPATH = "/Prestazione/@cod_servizio";

            string uri = IOHelper.ComposeSampleFilePath(FILE_NAME).Path;

            XPathList expectedXPaths = new XPathList(new[] { XPATH });
            List<string> expectedValues = new XPathList(new[] { "SM2" });

            XmlKeyValuePairCollection actual = XNavigator.Parse(uri, XPATH);

            XPathList actualXPaths = actual.XPaths;
            Assert.IsTrue(expectedXPaths.IsMatch(actualXPaths));

            List<string> actualValues = new XPathList(actual.Select(valuePair => valuePair.Value));
            Assert.IsTrue(expectedValues.CompareList(actualValues));
        }
    }
}